<?php
namespace view;
require_once 'controller/control.php';

class PageView{
	
	/* checks if logged in
	 * @return html
	 */
	public function checkIfLoggedIn($message){
		
		if(isset($_SESSION['loggedIn'])){
			$html = $this->loggedInPage($message);
		}else{
			$html = $this->printLoginForm($message);
		}
		return $html;
	}
	
	//@return html
	private function printLoginForm($message = null) {
	
	 	$html = '<!DOCTYPE html>
	 		<head>
	 		<meta charset="utf-8">
	 		<title>Benny är Awesome!</title>
	 		</head>
	 		<body>
	 		<h1>Ej Inloggad!</h1>
	 		';
			$html .= $message;
			
		$usernameValue = null;
		if (isset($_POST['username'])) {
			$usernameValue = $_POST['username'];	
		}
	 	$html .= 
	 		"<form id='logInForm' action='index.php?attempt' method='post'>
	 		Användarnamn: 
	 		<input type='text' Name='username' id='username' value='$usernameValue' />";		
	 	$html .=	
	 		'Lösenord: 
	 		<input type="password" Name="password" id="password" />
	 		<input type="checkbox" name="checkbox" value="1" id="checkbox"/>Remember Me!
	 		<button type="submit" name="logInButton">Log In</button>
	 		</form>
	 		'. $this->theTime() .'
	 		</body>
	 		</html>';
	 	return $html;
	}
	
	//@return html
	private function loggedInPage($message = null){
		
 		$html = '<!DOCTYPE html>
 		<meta charset="utf-8">
    	<body>';
		
		if ($message != null){
			$html .= $message;
			$message = null;
			}
		
	 	$html .='	
	 		<h1>Admin är inloggad!</h1>
	 		<form action="index.php" method="post">
	 			<button type="buttton" name="logOutButton" 
	 			id="logOutButton">Log Out
	 			</button>
			</form>
			'. $this->theTime() .'
	 		</body>
	 		</html>
	 		';
	 
	 	return $html;
	}
	
	//@return string (time)
	private function theTime(){
			
		$daynum = date("j");
		$year   = date("Y");
		$hours  = date("H");
		$minutes= date("i");
		$seconds= date("s");
	
	 	$day = $this->getDay();
		$month = $this->getMonth();
	 
		$klocka = $day . " den " . $daynum . " " . $month . "  år " . $year .
				" Klockan är [". $hours . ":" . $minutes . ":" . $seconds . "].";
	
		return $klocka;
	}
	//@return the day "string"
	private function getDay(){
			
		$day    = date("l");
		
		switch($day)
		{
		case "Monday":    $day = "Måndag";  break;
		case "Tuesday":   $day = "Tisdag"; break;
		case "Wednesday": $day = "Onsdag";  break;
		case "Thursday":  $day = "Torsdag"; break;
		case "Friday":    $day = "Fredag";  break;
		case "Saturday":  $day = "Lördag";  break;
		case "Sunday":    $day = "Söndag";  break;
		}
		
		return $day;
	}
	//returns the month "string"
	private function getMonth(){
		
		$month  = date("F");
	
		switch($month)
		{
		case "January":   $month = "Januari";    break;
		case "February":  $month = "Februari";   break;
		case "March":     $month = "Mars";     break;
		case "April":     $month = "April";     break;
		case "May":       $month = "Maj";       break;
		case "June":      $month = "Juni";      break;
		case "July":      $month = "Juli";      break;
		case "August":    $month = "Augusti";    break;
		case "September": $month = "September"; break;
		case "October":   $month = "Oktober";   break;
		case "November":  $month = "November";  break;
		case "December":  $month = "December";  break;
		}
	
		return $month;
	}
}
